﻿#region File Description
//-----------------------------------------------------------------------------
// BusinessManagingScreen.cs
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ManagerGame;
using System.Collections.Generic;
using System.Diagnostics;
#endregion

namespace ManagerGame.Screens
{
    /// <summary>
    /// This is screen for managing all business
    /// </summary>
    class BusinessManagingScreen : MenuScreen
    {

        string menuTitle;
        Texture2D gradientTexture;
        Company companyManaged;
        MenuEntry workersMenuEntry;
        MenuEntry salaryMenuEntry;
        MenuEntry specialTreatmentMenuEntry;

        BusinessType _companyType;
        // 
        static int[] amountOfWorkers = { 1, 5, 10, 20 };
        static int currentAmountOfWorkers = 0; // this is index for the table "amountOfWorkers", so default is 1

        static int[] salary = { 30, 50, 100, 250, 500 };

        static int currentSalary = 0; // this is index for the salary - table

        static string[] specialTreatment = new string[2];


        static int specialTreatmentIndex = 0;

        public BusinessManagingScreen(string header, Company company, BusinessType companyType)
            : base(header)
        {
            companyManaged = company;
            _companyType = companyType;

            currentAmountOfWorkers = companyManaged.WorkerAmountIndex;
            currentSalary = companyManaged.SalaryAmountIndex;
            specialTreatmentIndex = companyManaged.SpecialTreatmentIndex;

            // debugging:
            Debug.WriteLine("BusinessManagingScreen --- CompanyID:" + companyManaged.Id);

            menuTitle = header;
            // Create our business menu entries.
            workersMenuEntry = new MenuEntry(string.Empty);
            salaryMenuEntry = new MenuEntry(string.Empty);
            specialTreatmentMenuEntry = new MenuEntry(string.Empty);
            MenuEntry playGameMenuEntry = new MenuEntry("Done");



            SetMenuEntryText();


            // Hook up menu event handlers.
            workersMenuEntry.Selected += amountOfWorkersMenuEntrySelected;
            salaryMenuEntry.Selected += salaryMenuEntrySelected;
            specialTreatmentMenuEntry.Selected += specialTreatmentMenuEntrySelected;
            playGameMenuEntry.Selected += doneMenuEntrySelected;

            IsPopup = false;


            MenuEntries.Add(workersMenuEntry);
            MenuEntries.Add(salaryMenuEntry);
            MenuEntries.Add(specialTreatmentMenuEntry);
            MenuEntries.Add(playGameMenuEntry);
        }

        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            workersMenuEntry.Text = "Workers: " + amountOfWorkers[currentAmountOfWorkers];
            salaryMenuEntry.Text = "Salary: " + salary[currentSalary] + " in a day / employee";


            specialTreatment[0] = "Nothing.";
            string treatment = string.Empty;
            switch (_companyType)
            {
                case BusinessType.Pub:
                    treatment = "Sauna (and beer!)";
                    break;
                case BusinessType.PimpCave:
                    treatment = "Diamonds are a girl's best friends!";
                    break;
                case BusinessType.DrugJoint:
                    treatment = "EVE = Extra violent encouraging...";
                    break;
                default:
                    break;
            }
            specialTreatment[0] = "Nothing";
            specialTreatment[1] = treatment;
            specialTreatmentMenuEntry.Text = "Special care for your stuff: " + specialTreatment[specialTreatmentIndex];
        }


        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            GraphicsDevice graphics = ScreenManager.GraphicsDevice;

            // Darken down any other screens that were drawn beneath the popup.
            // ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);



            Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192) * TransitionAlpha;


            Rectangle backgroundRectangle = new Rectangle();
            // Fade the popup alpha during transitions.
            Color color = Color.Green * TransitionAlpha;

            spriteBatch.Begin();

            // Draw the background rectangle.
            spriteBatch.Draw(gradientTexture, backgroundRectangle, color);


            spriteBatch.End();
        }
        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            if (Keyboard.GetState().IsKeyDown(Keys.X))
            {
                ExitScreen();
            }
        }

        public override void LoadContent()
        {
            base.LoadContent();
            ContentManager content = ScreenManager.Game.Content;
            gradientTexture = content.Load<Texture2D>("Images\\gradient");
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }


        void amountOfWorkersMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentAmountOfWorkers = (currentAmountOfWorkers + 1) % amountOfWorkers.Length;

            SetMenuEntryText();
            if (companyManaged == null)
            {
                return; // todo: "No company..."
            }
            companyManaged.Workers = amountOfWorkers[currentAmountOfWorkers];
            companyManaged.WorkerAmountIndex = currentAmountOfWorkers;

        }

        void salaryMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentSalary = (currentSalary + 1) % salary.Length;

            SetMenuEntryText();
            if (companyManaged == null)
            {
                return; // todo: "No company..."
            }
            companyManaged.Salary = salary[currentSalary];
            companyManaged.SalaryAmountIndex = currentSalary;
        }

        void specialTreatmentMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            specialTreatmentIndex = (specialTreatmentIndex + 1) % specialTreatment.Length;
            SetMenuEntryText();
            if (companyManaged == null)
            {
                return; // todo: "No company..."
            }
            companyManaged.OperationLaunched = specialTreatmentIndex == 0 ? false : true;
            companyManaged.SpecialTreatmentIndex = specialTreatmentIndex;
        }

        void doneMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ExitScreen();

        }

    }
}
